package com.ctms.system.controller;

import com.ctms.common.controller.BaseController;
import com.ctms.common.utils.MD5Utils;
import com.ctms.common.utils.PageUtils;
import com.ctms.common.utils.Query;
import com.ctms.common.utils.R;
import com.ctms.system.domain.RoleDO;
import com.ctms.system.domain.UserDO;
import com.ctms.system.domain.UserRoleDO;
import com.ctms.system.service.RoleService;
import com.ctms.system.service.StudentService;
import com.ctms.system.service.TeacherService;
import com.ctms.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("/sys/user")
@Controller
public class UserController extends BaseController {
	@Autowired
	UserService userService;
	@Autowired
	RoleService roleService;
	@Autowired
	StudentService studentService;
	@Autowired
	TeacherService teacherService;

	@RequiresPermissions("sys:user:user")
	@GetMapping("")
	String user(Model model) {
		return "sys/user/user";
	}

	@GetMapping("/list")
	@ResponseBody
	PageUtils list(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);
		List<UserDO> userCommDoList = userService.selectByName(query);
		int total = userService.count(query);
		PageUtils pageUtil = new PageUtils(userCommDoList, total);
		return pageUtil;
	}

	@RequiresPermissions("sys:user:add")
	//@Log("添加用户")
	@GetMapping("/add")
	String add(Model model) {
		List<RoleDO> roles = roleService.list();
		model.addAttribute("roles", roles);
		return "sys/user/add";
	}

	@RequiresPermissions("sys:user:edit")
	//@Log("编辑用户")
	@GetMapping("/edit/{id}")
	String edit(Model model, @PathVariable("id") Long id) {
		UserDO userDO = userService.get(id);
		model.addAttribute("user", userDO);
		List<RoleDO> roles = roleService.list(id);
		model.addAttribute("roles", roles);
		return "sys/user/edit";
	}

	@RequiresPermissions("sys:user:add")
	//@Log("保存用户")
	@PostMapping("/save")
	@ResponseBody
	R save(UserDO user) {
		if ("test"==getUsername()) {
			return R.error(1, "演示系统不允许删除,完整体验请部署程序");
		}
		user.setPassword(MD5Utils.encrypt(user.getUsername(), user.getPassword()));
		//获取角色id
		int roleId = user.getRoleId();

		//设置身份
		user.setIdentity(roleId-1);
		Date date = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String create_time = simpleDateFormat.format(date);
		user.setGmtCreate(create_time);
		if (userService.save(user) > 0) {
			int user_id = studentService.selectMaxIdByTime();
			int role = roleId;
			//插入用户角色表
			UserRoleDO userRoleDO = new UserRoleDO();
			userRoleDO.setUserId(Long.parseLong(Integer.toString(user_id)));
			userRoleDO.setRoleId(Long.parseLong(Integer.toString(roleId)));
			teacherService.insertUserRole(userRoleDO);
			return R.ok();
		}
		return R.error();
	}

	@RequiresPermissions("sys:user:edit")
//	@Log("更新用户")
	@PostMapping("/update")
	@ResponseBody
	R update(UserDO user) {
		// return R.error(1, "演示系统不允许修改");
		Date date = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String update_time = simpleDateFormat.format(date);
		user.setGmtModified(update_time);
		if (userService.update(user) > 0) {
			return R.ok();
		}
		return R.error();
	}

	@RequiresPermissions("sys:user:remove")
	//@Log("删除用户")
	@PostMapping("/remove")
	@ResponseBody
	R remove(Long id) {
		// return R.error("演示系统不允许删除");
		if (userService.remove(id) > 0) {
			return R.ok();
		}
		return R.error();
	}

	@RequiresPermissions("sys:user:batchRemove")
	//@Log("批量删除用户")
	@PostMapping("/batchRemove")
	@ResponseBody
	R batchRemove(@RequestParam("ids[]") Long[] userIds) {
		// return R.error("演示系统不允许删除");
		List<Long> Ids = Arrays.asList(userIds);
		int r = userService.batchremove(Ids);
		System.out.println(r);
		if (r > 0) {
			return R.ok();
		}
		return R.error();
	}

	@PostMapping("/exit")
	@ResponseBody
	boolean exit(@RequestParam Map<String, Object> params) {
		// Query query = new Query(params);
		return !userService.exit(params);// 存在，不通过，false
	}


	//@Log("提交更改用户密码")
	@PostMapping("/resetPwd")
	@ResponseBody
	R resetPwd(Integer userId) {
		//通过管理员选择的用户id查找该用户信息
		UserDO userDO = userService.selectUserById(userId);
		String username = userDO.getUsername();
		String password = "123456";
		//重新设置密码
		userDO.setPassword(MD5Utils.encrypt(username, password));
		//进行修改密码的操作
		if (userService.resetPwd(userDO) > 0) {
			//密码重置成功
			return R.ok();
		}
		//密码重置失败
		return R.error();
	}

}
